﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton_7,&QPushButton::clicked,this,&MainWindow::on_pushbutton_7_clicked);
    connect(this,&MainWindow::wxx_signal,this,&MainWindow::outString);
    pre_pos=QPoint(0,0);
    color=Qt::red;
    ui->horizontalSlider->setRange(0,10);
    ui->label->setText("线条粗细");
    ui->lcdNumber->setStyleSheet("color: blue; background-color: black;");//色为黑色
    QFont font=ui->lcdNumber->font();
    font.setBold(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);
    QPen pen=paint.pen();
    pen.setWidth(lineSize);
    pen.setColor(color);
    paint.setPen(pen);
    wxx_signal("主窗口被重绘");
    switch (it) {
    case line:
        paint.drawLine(0,0,200,200);
        break;
    case rect:
        paint.drawRect(0,0,300,200);
        break;
    case circle:
        paint.drawEllipse(0,0,400,400);
        break;
    case fiveagon:
    {
        QPoint parr[5];
        parr[0] = QPoint(100, 0);
        parr[1] = QPoint(75, 25);
        parr[2] = QPoint(50, 50);
        parr[3] = QPoint(25, 75);
        parr[4] = QPoint(0, 100);
        paint.drawPolygon(parr,5);
        break;
    }
    case path:
        paint.drawLines(track);
        break;
    default:
        break;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pos=event->pos();
    QLine line=QLine(pre_pos,pos);
    track.push_back(line);
    pre_pos=pos;
    repaint();
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    switch (event->button()) {
    case Qt::LeftButton:
        qDebug("mouse left button pressed");
        break;
    case Qt::MiddleButton:
        qDebug("mouse wheel pressed");
        break;
    case Qt::RightButton:
        qDebug("mouse right button pressed");
        break;
    default:
        qDebug("no thing pressed");
        break;
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    switch (event->button()) {
    case Qt::LeftButton:
        qDebug("mouse left button released");
        break;
    case Qt::MiddleButton:
        qDebug("mouse wheel released");
        break;
    case Qt::RightButton:
        qDebug("mouse right button released");
        break;
    default:
        qDebug("no thing released");
        break;
    }
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    qDebug("我被双击了");
}

void MainWindow::enterEvent(QEvent *event)
{
    qDebug("鼠标进入主窗口");
}

void MainWindow::leaveEvent(QEvent *event)
{
    qDebug("鼠标离开主窗口");
}

bool MainWindow::event(QEvent *event)
{
    if(event->type()==QEvent::MouseButtonPress)
    {
        qDebug("说表按下");
        return QMainWindow::event(event);
    }
    return QMainWindow::event(event);
}


void MainWindow::on_pushButton_clicked()
{
    it=line;
    update();
}

void MainWindow::on_pushButton_2_clicked()
{
    it=circle;
    update();
}



void MainWindow::on_pushButton_3_clicked()
{
    it=rect;
    qDebug("画矩形");
    update();
}

void MainWindow::on_pushButton_4_clicked()
{
    it=fiveagon;
    update();
}

void MainWindow::on_pushButton_5_clicked()
{
    it=path;
    setMouseTracking(true);
    ui->centralwidget->setMouseTracking(true);
    update();
}


void MainWindow::on_pushButton_6_clicked()
{
    QColor color_1=QColorDialog::getColor(
                Qt::red,
                this,
                "选择颜色"
                );
    if(color_1.isValid())
    {
        qDebug()<<"选择的颜色是:"<<color;
        color=color_1;
        repaint();
    }
    else
    {
        qDebug()<<"没有选择颜色";
    }
}

void MainWindow::on_pushbutton_7_clicked()
{
    it=clear;
    update();
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    lineSize=value;
    ui->lcdNumber->display(value);
    repaint();
}

void MainWindow::outString(QString str)
{
    qDebug()<<str;
}
